package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.musicplayer.MusicScoreManager;
import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MusicScoreItemAdapter;
import com.example.musicplayer.dialogTools.EditMusicScoreDialog;
import com.example.musicplayer.dialogTools.SortMusicScoreDialog;

public class MusicScoreActivity extends BaseSettingsActivity {
    private ListView mListView;
    private MusicScoreItemAdapter mAdapter;
    private boolean mEditMode;

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setEnableMusicTabView(false);
        mEditMode = false;
        mListView = (ListView) LayoutInflater.from(this).inflate(R.layout.list_view, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , 0);
        lp.weight = 1;
        ((LinearLayout) findViewById(R.id.linear_container)).addView(mListView, lp);
        //add footer view
        View footerView = LayoutInflater.from(this).inflate(R.layout.empty_view, null);
        footerView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.empty_view_height)));
        footerView.setEnabled(false);
        mListView.addFooterView(footerView);
        //init list view
        MusicScoreManager manager = MusicScoreManager.getInstance();
        if (manager != null) {
            manager.init();
            mAdapter = new MusicScoreItemAdapter(this);
            mListView.setAdapter(mAdapter);
        }
        updateToolbarText();
        findViewById(R.id.toolbar_text_right).setOnClickListener(v -> {
            mEditMode = !mEditMode;
            updateToolbarText();
        });
        findViewById(R.id.toolbar_text_right_second).setOnClickListener(v -> {
            showSortMusicScoreDialog();
        });
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            if (mEditMode) {
                showEditMusicScoreDialog(position);
            } else {
                Intent intent = new Intent(MusicScoreActivity.this, MusicDetailsActivity.class);
                intent.putExtra("position", position);
                MusicScoreManager.MusicScoreInfo musicScoreInfo = (MusicScoreManager.MusicScoreInfo) mAdapter.getItem(position);
                if (musicScoreInfo != null) {
                    intent.putExtra("musicId", musicScoreInfo.getID());
                }
                startActivityForResult(intent, 1);
            }
        });
        mListView.setOnItemLongClickListener((parent, view, position, id) -> {
            mEditMode = true;
            updateToolbarText();
            showEditMusicScoreDialog(position);
            return true;
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (MusicScoreManager.getInstance() != null) {
            MusicScoreManager.getInstance().destroy();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == MusicDetailsActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra(MusicDetailsActivity.EDIT_MUSIC_SCORE, false)) {
                int position = data.getIntExtra("position", -1);
                if (mAdapter != null) {
                    mAdapter.notifyMusicScoreChanged(position);
                }
            }
        }
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_music_score_activity;
    }

    private void updateToolbarText() {
        TextView editView = findViewById(R.id.toolbar_text_right);
        TextView sortView = findViewById(R.id.toolbar_text_right_second);
        if (mEditMode) {
            editView.setText(R.string.confirm);
            setActivityTitle(getString(R.string.title_music_score_activity_edit));
        } else {
            editView.setText(R.string.edit);
            setActivityTitle(getString(R.string.title_music_score_activity));
        }
        sortView.setText(R.string.music_score_activity_sort);
    }

    private void showEditMusicScoreDialog(int position) {
        if (mAdapter != null) {
            MusicScoreManager.MusicScoreInfo musicScoreInfo = (MusicScoreManager.MusicScoreInfo) mAdapter.getItem(position);
            final EditMusicScoreDialog dialog = new EditMusicScoreDialog(this, musicScoreInfo);
            dialog.setOnDataChangedListener(() -> mAdapter.notifyDataSetChanged());
            dialog.getDialog().setOnDismissListener(dialog1 -> mAdapter.notifyMusicScoreChanged(position));
            dialog.show();
        }
    }

    private void showSortMusicScoreDialog() {
        SortMusicScoreDialog dialog = new SortMusicScoreDialog(this);
        dialog.setOnConfirmListener((sortType, sortReverse, onlyScored) -> {
            MusicScoreManager manager = MusicScoreManager.getInstance();
            if (manager != null) {
                manager.setSortType(sortType);
                manager.setSortReverse(sortReverse);
                manager.setOnlyScored(onlyScored);
            }
            if (mAdapter != null) {
                mAdapter.notifyMusicSortChanged(sortType, sortReverse, onlyScored);
            }
        });
        dialog.show();
    }
}